const app = getApp();

Page({
  data: {
    src: "",
    volume: "50",
    tone: "50",
    speed: "50",
    text: "语音合唱功能真不错呢！",
    tte: "UTF8",
    vcn: "xiaoyan",
  },
  volume(e) {
    this.setData({
      volume: e.detail.value,
    });
  },
  tone(e) {
    this.setData({
      tone: e.detail.value,
    });
  },
  speed(e) {
    this.setData({
      speed: e.detail.value,
    });
  },
  onLoad(options) {
    // 使用 wx.createAudioContext 获取 audio 上下文 context
    this.audioCtx = wx.createInnerAudioContext('myAudio');
    wx.setNavigationBarTitle({
      title: "语音合成"
    });
  },
  formSubmit(e) {
    console.log(e);
    var that = this;
    wx.request({
      url: 'http://localhost:8080/test/speech',
      method: "POST",
      data: {
        text: e.detail.value.text,
        tte: "UTF8",
        vcn: e.detail.value.radio,
        volume: e.detail.value.volume,
        pitch: e.detail.value.tone,
        speed: e.detail.value.speed,
      },
      dataType: "json",
      success: function (result) {
        console.log(result);
        that.audioCtx.src = result.data;
        that.audioCtx.play();
      }
    });
  },
  audioPlay: function () {
    this.audioCtx.play();
  },
  audioPause: function () {
    this.audioCtx.pause();
  },
  audio5: function () {
    this.audioCtx.seek(5);
  },
  audioStart: function () {
    this.audioCtx.seek(0);
  }
});